package yl.hs.bmipfull.service.dataCockpit.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import yl.hs.bmipfull.daos.finance.IReceiptSettleMainDao;
import yl.hs.bmipfull.daos.finance.IReceivePaymentRecordDao;
import yl.hs.bmipfull.daos.finance.IShipInvoiceRecordDao;
import yl.hs.bmipfull.daos.finance.IShipSettleMainDao;
import yl.hs.bmipfull.daos.material.IPurchaseReceiptDao;
import yl.hs.bmipfull.daos.material.IRawMaterialCateDao;
import yl.hs.bmipfull.daos.production.IProductShipOrderDao;
import yl.hs.bmipfull.daos.production.IProductionRecordDao;
import yl.hs.bmipfull.daos.salemanage.ISaleContractDao;
import yl.hs.bmipfull.daos.salemanage.ISaleOrderDao;
import yl.hs.bmipfull.pojo.finance.ReceiptSettleMainEntity;
import yl.hs.bmipfull.pojo.finance.ShipSettleMainEntity;
import yl.hs.bmipfull.service.dataCockpit.IFinancialDataService;
import yl.hs.bmipfull.utils.CK;
import yl.hs.bmipfull.utils.HsApp;
import yl.hs.bmipfull.viewmodels.dataCockpit.CollectionVo;

import java.math.BigDecimal;
import java.util.*;

/**
 * @author 韩纪旺
 * @apiNote
 * @since 2022/9/22 17:15 创建
 */
@Service
public class FinancialDataServiceImpl implements IFinancialDataService {


    @Autowired
    ISaleOrderDao dao;

    @Autowired
    IProductionRecordDao RecordDao;
    @Autowired
    IReceiptSettleMainDao mainDao;


    @Autowired
    IProductShipOrderDao orderDao;

    @Autowired
    IReceivePaymentRecordDao PaymentRecordDao;

    @Autowired
    IShipSettleMainDao MainDao;

    @Autowired
    IShipInvoiceRecordDao recordDao;

    @Autowired
    ISaleContractDao contractDao;

    @Autowired
    IPurchaseReceiptDao receiptDao;

    @Autowired
    IRawMaterialCateDao rawMaterialCateDao;

    //销售量


    @Override
    public Object getSalesVolume(String timeType) {
        List<Object> List = new ArrayList<>();
        Map<String, Object> map = new HashMap<>();
        Map<String, Object> map1 = new HashMap<>();
        Map<String, Object> map2 = new HashMap<>();
        String BetonSysCode = HsApp.getSystemSet(CK.BetonSysCode);
        String MortarSnSysCode = HsApp.getSystemSet(CK.MortarSnSysCode);
        String SaleRawMatailSN = HsApp.getSystemSet(CK.SaleRawMatailSN);
        var current = dao.getSalesVolume(BetonSysCode, timeType);
        var annular = dao.getAnnularPeriod(BetonSysCode, timeType);
        map.put("type", "商砼");
        map.put("current", current);
        map.put("Annular", annular);
        var current1 = dao.getSalesVolume(MortarSnSysCode, timeType);
        var annular1 = dao.getAnnularPeriod(MortarSnSysCode, timeType);
        map1.put("type", "砂浆");
        map1.put("current", current1);
        map1.put("Annular", annular1);
        var current2 = dao.getSalesVolume(SaleRawMatailSN, timeType);
        var annular2 = dao.getAnnularPeriod(SaleRawMatailSN, timeType);
        map2.put("type", "外卖原材");
        map2.put("current", current2);
        map2.put("Annular", annular2);
        List.add(map);
        List.add(map1);
        List.add(map2);

        return List;

    }

    //产值
    @Override
    public Object getOutputValue(String timeType) {

        List<Object> List = new ArrayList<>();
        Map<String, Object> map = new HashMap<>();
        Map<String, Object> map1 = new HashMap<>();
        Map<String, Object> map2 = new HashMap<>();
        String BetonSysCode = HsApp.getSystemSet(CK.BetonSysCode);
        String MortarSnSysCode = HsApp.getSystemSet(CK.MortarSnSysCode);
        String SaleRawMatailSN = HsApp.getSystemSet(CK.SaleRawMatailSN);
        var BetonSys = orderDao.getOutputValue(BetonSysCode, timeType);
        var SysCode = orderDao.getPeriod(BetonSysCode, timeType);
        map.put("type", "商砼");
        map.put("current", BetonSys);
        map.put("Annular", SysCode);
        var BetonSys1 = orderDao.getOutputValue(MortarSnSysCode, timeType);
        var SysCode1 = orderDao.getPeriod(MortarSnSysCode, timeType);
        map1.put("type", "砂浆");
        map1.put("current", BetonSys1);
        map1.put("Annular", SysCode1);
        var BetonSys2 = orderDao.getOutputValue(SaleRawMatailSN, timeType);
        var SysCode2 = orderDao.getPeriod(SaleRawMatailSN, timeType);
        map2.put("type", "外卖原材");
        map2.put("current", BetonSys2);
        map2.put("Annular", SysCode2);
        List.add(map);
        List.add(map1);
        List.add(map2);

        return List;

    }

    @Override
    public Map<String, Object> getMonthlyOutputValue() {
        Calendar cNow = Now();
        Calendar cLast = Last();
        //月份集合
        List<String> monthStr=new ArrayList<>();
        //本期每月重量
        List<Object> nowNetWeight = new ArrayList<>();
        //上期每月重量
        List<Object> oldNetWeight = new ArrayList<>();
        //循环查询1-12月销售重量（本期与上期）
        for (int i = 0; i < 12; i++) {
            if (i>0){
                cNow.add(Calendar.MONTH,1);
                cLast.add(Calendar.MONTH,1);
            }
            monthStr.add((i + 1) + "月");
            nowNetWeight.add(orderDao.getMonthlyOutputValue(cNow.getTime()));
            oldNetWeight.add(orderDao.getMonthlyOutputValue(cLast.getTime()));
        }
        //返回值设值
        Map<String, Object> result = new HashMap<>();
        result.put("monthStr", monthStr);
        result.put("nowNetWeight", nowNetWeight);
        result.put("oldNetWeight", oldNetWeight);
        return result;
    }




    @Override
    public Map<Object, Object> getCollection() {
        Map<Object, Object> map = new HashMap();
        //本月结算应收金额
        BigDecimal settlement = MainDao.getSettlement();
        //本月累计应收款金额
        BigDecimal LastMonthSubtract = PaymentRecordDao.getLastMonthSubtract();
        BigDecimal add = settlement.add(LastMonthSubtract);

        //本月预结算应收金额
        BigDecimal preSettlement = MainDao.getPreSettlement();
        //本月实收金额
        BigDecimal monthCollection = PaymentRecordDao.getMonthCollection();
        //本月逾期未收金额
        BigDecimal subtract = settlement.subtract(monthCollection);

        map.put("settlement",settlement);
        map.put("add",add);
        map.put("preSettlement",preSettlement);
        map.put("subtract",subtract);
        map.put("monthCollection",monthCollection);
        return map;


    }

    @Override
    public Map<String, Object> getMonthlyCollectionTrend() {
        Calendar cNow = Now();
        Calendar cLast = Last();
        //月份集合
        List<String> monthStr = new ArrayList<>();
        //本期每月重量
        List<Object> nowNetWeight = new ArrayList<>();
        //上期每月重量
        List<Object> oldNetWeight = new ArrayList<>();
        //循环查询1-12月销售重量（本期与上期）
        for (int i = 0; i < 12; i++) {
            if (i > 0) {
                cNow.add(Calendar.MONTH, 1);
                cLast.add(Calendar.MONTH, 1);
            }
            monthStr.add((i + 1) + "月");
            nowNetWeight.add(PaymentRecordDao.getMonthlyCollectionTrend(cNow.getTime()));
            oldNetWeight.add(PaymentRecordDao.getMonthlyCollectionTrend(cLast.getTime()));
        }
        //返回值设值
        Map<String, Object> result = new HashMap<>();
        result.put("monthStr", monthStr);
        result.put("nowNetWeight", nowNetWeight);
        result.put("oldNetWeight", oldNetWeight);
        return result;
    }



    @Override
    public Object customerCollectionRate() {
        List<Object> list=new ArrayList<>();
       var l= MainDao.getAll();
        for (ShipSettleMainEntity ship : l) {
            CollectionVo vo=new CollectionVo();
            var shiShou = PaymentRecordDao.getNetReceipts(ship.getContractSN());
            vo.setName(ship.getCustomerName());
            vo.setShiShou(shiShou);
            BigDecimal yingShou = ship.getTotalReceivedAmount();
            vo.setYingShou(yingShou);
            if(shiShou.compareTo(BigDecimal.ZERO)==0||yingShou.compareTo(BigDecimal.ZERO)==0){
                vo.setCollectionRate(BigDecimal.ZERO);
            }else{
                vo.setCollectionRate(shiShou.divide(yingShou).multiply(new BigDecimal(100)));
            }
            list.add(vo);
        }
            return list;
    }

    @Override
    public Object getPurchaseVolume(String timeType) {
        Map<Object,Object> result=new HashMap<>();
        List<Object> list=new ArrayList<>();
       //当期
        var  purchaseVolume= receiptDao.getPurchaseVolume(timeType);
        //环期
        var annularPurchaseVolume=receiptDao.getAnnularPurchaseVolume(timeType);

            result.put("current",purchaseVolume);
            result.put("Annular",annularPurchaseVolume);
            list.add(result);
            return list;

    }

    @Override
    public Object getPurchaseCost(String timeType) {

        Map<Object,Object> result=new HashMap<>();
        List<Object> list=new ArrayList<>();
        //当期
        var  purchaseVolume= receiptDao.getPurchaseCost(timeType);
        //环期
        var annularPurchaseVolume=receiptDao.getPurchaseAnnular(timeType);

        result.put("current",purchaseVolume);
        result.put("Annular",annularPurchaseVolume);
        list.add(result);
        return list;

    }

    @Override
    public Map<String, Object> getPurchaseTrend() {
        Calendar cNow = Now();
        Calendar cLast = Last();
        //月份集合
        List<String> monthStr=new ArrayList<>();
        //本期每月重量
        List<Object> nowNetWeight = new ArrayList<>();
        //上期每月重量
        List<Object> oldNetWeight = new ArrayList<>();
        //循环查询1-12月销售重量（本期与上期）
        for (int i = 0; i < 12; i++) {
            if (i>0){
                cNow.add(Calendar.MONTH,1);
                cLast.add(Calendar.MONTH,1);
            }
            monthStr.add((i + 1) + "月");
            nowNetWeight.add(orderDao.getPurchaseTrend(cNow.getTime()));
            oldNetWeight.add(orderDao.getPurchaseTrend(cLast.getTime()));
        }
        //返回值设值
        Map<String, Object> result = new HashMap<>();
        result.put("monthStr", monthStr);
        result.put("nowNetWeight", nowNetWeight);
        result.put("oldNetWeight", oldNetWeight);
        return result;

    }
    @Override
    public Map<Object, Object> getPaymentSituation() {
        Map<Object, Object> map=new HashMap<>();
        ArrayList<Object> l = new ArrayList<>();
        ArrayList<Object> list = new ArrayList<>();
        //获取本月结算应付
        var settlement = PaymentRecordDao.getPayableMonth();
        //本月预结算应付金额
        var preSettlement=  MainDao.getPreSettlementCopeWith();
        //本月实付金额
        var  monthCollection = PaymentRecordDao.getPaidMonthContractSN();
        //本月逾期未付金额
        var subtract = settlement.subtract(monthCollection);
        //本月累计应付款金额（本月结算应付+上期未付）CumulativeCopeWith
        var cumulativeCopeWith= PaymentRecordDao.getCumulativeCopeWith();
        BigDecimal add = settlement.add(cumulativeCopeWith);
        map.put("settlement",settlement);
        map.put("preSettlement",preSettlement);
        map.put("monthCollection",monthCollection);
        map.put("subtract",subtract);
        map.put("add",add);
        return map;
    }

    @Override
    public Map<String, Object> getMonthlyPayment() {
        Calendar cNow = Now();
        Calendar cLast = Last();
        //月份集合
        List<String> monthStr=new ArrayList<>();
        //本期每月重量
        List<Object> nowNetWeight = new ArrayList<>();
        //上期每月重量
        List<Object> oldNetWeight = new ArrayList<>();
        //循环查询1-12月销售重量（本期与上期）
        for (int i = 0; i < 12; i++) {
            if (i>0){
                cNow.add(Calendar.MONTH,1);
                cLast.add(Calendar.MONTH,1);
            }
            monthStr.add((i + 1) + "月");
            nowNetWeight.add(PaymentRecordDao.getMonthlyPayment( cNow.getTime()));
            oldNetWeight.add(PaymentRecordDao.getMonthlyPayment(cLast.getTime()));

            }


        //返回值设值
        Map<String, Object> result = new HashMap<>();
        result.put("monthStr", monthStr);
        result.put("nowNetWeight", nowNetWeight);
        result.put("oldNetWeight", oldNetWeight);
        return result;

    }


    public  Calendar Now(){
        //今年1月，将cNow对象时间设置为本年一月份
        Calendar cNow = Calendar.getInstance();
        cNow.setTime(new Date());
        cNow.clear(Calendar.MONTH);
        return cNow;
    }

    public   Calendar Last(){
        //去年1月,将cLast对象时间设置为本年一月份
        Calendar cLast = Calendar.getInstance();
        cLast.setTime(new Date());
        cLast.clear(Calendar.MONTH);
        cLast.add(Calendar.YEAR, -1);
        return cLast;
    }

    @Override
    public Object selectPenceRate() {

        List<Object> list=new ArrayList<>();
        var l= mainDao.getAll();
        for (ReceiptSettleMainEntity ship : l) {
            CollectionVo vo=new CollectionVo();
            var shiFu = PaymentRecordDao.getNetReceiptsFu(ship.getContractSN());
            vo.setName(ship.getSupplierName());
            vo.setShiShou(shiFu);
            BigDecimal yingShou = ship.getTotalPayedAmount();
            vo.setYingShou(yingShou);
            if(shiFu.compareTo(BigDecimal.ZERO)==0||yingShou.compareTo(BigDecimal.ZERO)==0){
                vo.setCollectionRate(BigDecimal.ZERO);
            }else{
                vo.setCollectionRate(shiFu.divide(yingShou).multiply(new BigDecimal(100)));
            }
            list.add(vo);
        }
        return list;
    }

}
